# Collect Flickr Data with Python FlickrAPI
# flickrapi path is required to be added to system path first
# Scientific Visulization and Virtual Reality
# Wouter Josemans and Ninghang Hu
# Oct 21, 2010

import time
import datetime
import sys;
sys.path.append('flickrapi-1.4.2./')
import flickrapi
sys.path.append('GetGoogleMap/')
from pythonmap import *

# Map Parameters
filename = 'AmsterdamFlickr'
zoom = 10

START = datetime.datetime(2005,01,01)
END = datetime.datetime(2010,10,19)
delta = datetime.timedelta(days=7)

# Find bounding box lat/lon coordinates of map
bboxP = "4.810638427734375,52.3154051429798,4.9774932861328125,52.3923633970718"
lat = ("52.3154051429798","52.3923633970718")
lon = ("4.810638427734375","4.9774932861328125")

# Download google map
    #gmap = stitch.StitchedMap(lat,lon,-1,zoom,'satellite')
    #map_name = gmap.generate()
    #print "google map completed"

# start flickr
flickr = flickrapi.FlickrAPI('6b03a335bca86810f4ba931e8ef646ed')

file1 = open(filename+'.txt','w')

# search flickr photos for each time span
week = 1
start_date = START
end_date = start_date + delta
while end_date < END :
    print '-----------'+str(week)+'/'+str((END-START).days/7)+'-----------'
    # get number of pages/perpage
    photos=flickr.photos_search(min_taken_date="%s-%s-%s" % (start_date.year,start_date.month,start_date.day),\
            max_taken_date="%s-%s-%s" % (end_date.year,end_date.month,end_date.day),\
            bbox=bboxP,\
            page='1',\
            perpage='250',\
            extras='geo,date_taken')

    # print debug info
    photos = photos[0]
    pages = int(photos.attrib['pages'])
    perpage = int(photos.attrib['perpage'])
#    print "Total pages:",pages
#    print "Total number:",photos.attrib['total']
#    print "Photos per Page",perpage

    # get data per page
    iter = 1
    while iter <= min(pages,16):
        try:
            print "processing page",iter,"/",pages
    #            print len(ID)
    #            if len(ID) != 0:
    #                print "unique items:" ,len(set(ID))\
    #                        ,"/",len(ID),"/",100*len(set(ID))/len(ID),"%"
            #else:
            #    print "unique items:" ,len(set(ID))\
                    #        ,"/",len(ID)
            photos = flickr.photos_search(\
                    min_taken_date="%s-%s-%s" % (start_date.year,start_date.month,start_date.day),\
                    max_taken_date="%s-%s-%s" % (end_date.year,end_date.month,end_date.day),\
                    bbox = bboxP,\
                    page = str(iter),\
                    perpage='250',\
                    extras='geo,date_taken')
            photos = photos[0]
            print len(photos)
            print "Total number:",photos.attrib['total']
            if len(photos) == 0:
                print "0 item found, pass"
            else:
                for photo in photos:
                    file1.write(photo.attrib['id']+';'+photo.attrib['longitude']+';'+photo.attrib['latitude']+';'+photo.attrib['datetaken']+'\n')
            iter += 1
            #time.sleep(2)
        except:
            print "connection error... getting current page again"
            time.sleep(10)
    
    start_date = start_date + delta
    end_date = start_date + delta
    week = week + 1
file1.close()
